//
//  ViewController.m
//  TestSearchBar
//
//  Created by 朱志佳 on 2018/4/10.
//  Copyright © 2018年 zhuzj. All rights reserved.
//

#import "ViewController.h"
#import "ZZJHeader.h"

#import "ZSearchBar.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //[self test];
    

}

- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
        [self test2];
}

- (void)test2{
    
    ZSearchBar *searchBar = [[ZSearchBar alloc] initWithFrame:CGRectMake(0, 100, IPHONEWIDTH, 36.0)];
    [self.view addSubview:searchBar];
}

-(void)test{
    
    ZSearchBar *searchBar = [[ZSearchBar alloc] initWithFrame:CGRectMake(0, 100, IPHONEWIDTH, 100)];
    
    /*
     typedef NS_ENUM(NSInteger, UIBarStyle) {
     UIBarStyleDefault         //白色搜索框，灰色背景
     UIBarStyleBlack           //黑色搜索框,
     }
     */
    searchBar.barStyle = UIBarStyleDefault;//搜索框风格

    //searchBar.prompt = @"prompt";// 搜索框顶部的提示信息
    searchBar.placeholder = @"placeholder";// 占位符，默认nil, 若有值则在输入文本后消失
    //searchBar.text = @"text";//搜索的文本
    
    searchBar.showsBookmarkButton = YES;// 搜索框右侧是否显示图书按钮
    searchBar.showsCancelButton = YES;//搜索框右侧是否显示取消按钮
    searchBar.showsSearchResultsButton = YES;//搜索框右侧是否显示搜索结果按钮
    searchBar.searchResultsButtonSelected = YES;// 搜索结果按钮为选中状态 //searchResultsButtonSelected = YES 效果，要结合showsSearchResultsButton = YES使用
    
    searchBar.tintColor = [UIColor orangeColor];//设置光标颜色  //可用于修改输入框的光标颜色，取消按钮和选择栏被选中时候都会变成设置的颜色
    searchBar.barTintColor = [UIColor grayColor];//设置搜索框背景颜色为灰色
    
    /*
     typedef NS_ENUM(NSUInteger, UISearchBarStyle) {
     UISearchBarStyleDefault,    // 默认样式，和UISearchBarStyleProminent 一样
     UISearchBarStyleProminent,  // 显示背景，常用在my Mail, Messages and Contacts
     UISearchBarStyleMinimal     // 不显示背景，系统自带的背景色无效，自定义的有效，常用在Calendar, Notes and Music
     }
     */
    searchBar.searchBarStyle = UISearchBarStyleDefault;// 搜索框样式
    
    //1.7搜索栏的附件选择按钮视图
    searchBar.showsScopeBar = YES;// 是否显示搜索栏的附件选择按钮视图
    searchBar.scopeButtonTitles = @[@"One", @"Two", @"Three"];// 选择按钮试图的按钮标题
    searchBar.selectedScopeButtonIndex = 1;// 选中的按钮下标值 ，默认 0. 如果超出范围则忽略
    
    //1.8搜索框背景图片、搜索框附属分栏条的背景颜色
//    UIImage *backgroundImage = [UIImage imageNamed:@"1"];
//    searchBar.backgroundImage = backgroundImage;//设置搜索框背景图片
//    UIImage *scopeBarBackgroundImage = [UIImage imageNamed:@"2"];//搜索框附属分栏条的背景颜色
//    searchBar.scopeBarBackgroundImage = scopeBarBackgroundImage;
    
    //2.1搜索框中文本框的背景偏移量和文本偏移量
    searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(5, 3);// 搜索框中文本框的背景偏移量
    searchBar.searchTextPositionAdjustment = UIOffsetMake(10, 5);// 搜索框中文本框的文本偏移量
    
    //2. UISearchBar 的方法
    
    //设置是否动画效果的显示或隐藏取消按钮
    //通常在开始编辑文本时将调用该方法，让取消按钮显示出来。
//    [searchBar setShowsCancelButton:YES animated:YES];/// 动画显示取消按钮
    
    //2.2 设置 (获取) 搜索框背景图片、选择按钮视图的背景图片、文本框的背景图片
//    // 1.设置搜索框背景图片
//    - (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics
//    //  获取置搜索框背景图片
//    - (nullable UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics
//    // 2.设置选择按钮视图的背景图片
//    - (void)setScopeBarButtonBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state
//    // 获取选择按钮视图的背景图片
//    - (nullable UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state
//    // 3.设置搜索框文本框的背景图片
//    - (void)setSearchFieldBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state
//    // 获取搜索框文本框的背景图片
//    - (nullable UIImage *)searchFieldBackgroundImageForState:(UIControlState)state
    // 设置搜索框背景图片
//    [searchBar setBackgroundImage:[UIImage imageNamed:@"1"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
//    // 设置选择按钮视图的背景图片
//    [searchBar setScopeBarButtonBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
//    // 设置搜索框文本框的背景图片
//    [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    
    //2.3设置（获取）搜索框的图标（包括搜索图标、清除输入的文字的图标、图书图标、搜索结果列表图标）
    // 设置搜索框的图标
//    - (void)setImage:(nullable UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state;
//    // 获取搜索框的图标
//    - (nullable UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state;
//    搜索框图标 UISearchBarIcon 有四个枚举值：
//    typedef NS_ENUM(NSInteger, UISearchBarIcon) {
//        UISearchBarIconSearch, // 搜索框放大镜图标
//        UISearchBarIconClear , // 搜索框右侧可用于清除输入的文字的图标x
//        UISearchBarIconBookmark , // 搜索框右侧的图书图标
//        UISearchBarIconResultsList , // 搜索框右侧的搜索结果列表图标
//    };
    // 设置搜索框放大镜图标
    UIImage *searchIcon = [UIImage imageNamed:@"searchIcon"];
    searchIcon = [UIImage imageNamed:@"1"];
    [searchBar setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    // 设置搜索结果列表图标
    UIImage *resultListIcon = [UIImage imageNamed:@"arrow"];
    resultListIcon = [UIImage imageNamed:@"2"];
    [searchBar setImage:resultListIcon forSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];
    
    //2.4设置（获取）选择按钮视图的分割线图片、按钮上显示的标题样式
//    // 设置选择按钮视图的分割线图片
//    - (void)setScopeBarButtonDividerImage:(nullable UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;
//    // 获取选择按钮视图的分割线图片
//    - (nullable UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;
//    // 设置选择按钮视图的标题样式
//    - (void)setScopeBarButtonTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state;
//    // 获取选择按钮视图的标题样式
//    - (nullable NSDictionary *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state
    // 设置选择按钮视图的分割线图片为一个橘色的线条
    //[searchBar setScopeBarButtonDividerImage:[UIImage imageNamed:@"divider"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
    // 设置选择按钮视图的标题样式为20号橘色空心的系统字体
    NSDictionary *attributeDic = @{NSFontAttributeName : [UIFont systemFontOfSize:20] , NSStrokeColorAttributeName : [UIColor orangeColor] , NSStrokeWidthAttributeName : @(3)};
    [searchBar setScopeBarButtonTitleTextAttributes:attributeDic forState:UIControlStateNormal];
    
    //2.5设置（获取）搜索框四种图标的偏移量
//    //  设置搜索框图标的偏移量
//    - (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon;
//    //  获取搜索框图标的偏移量
//    - (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon;
    [searchBar setPositionAdjustment:UIOffsetMake(15, 5) forSearchBarIcon:UISearchBarIconSearch];//设置搜索图标向有偏移15个单位，向下偏移5个单位后的效果图
    
    [self.view addSubview:searchBar];
    
}

#pragma mark -UISearchBarDelegate代理方法
//// 1. 将要开始编辑文本时会调用该方法，返回 NO 将不会变成第一响应者
//- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
//// 2. 开始输入文本会调用该方法
//- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;
//// 3. 将要结束编辑文本时会调用该方法，返回 NO 将不会释放第一响应者
//- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
//// 4. 结束编辑文本时调用该方法
//- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
//// 5. 文本改变会调用该方法（包含clear文本）
//- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
//// 6. 文字改变前会调用该方法，返回NO则不能加入新的编辑文字
//- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text ;
//// 7. 键盘上的搜索按钮点击的会调用该方法
//- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
//// 8. 搜索框右侧图书按钮点击会调用该方法
//- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar ;
//// 9.点击取消按钮会调用该方法
//- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;
//// 10.搜索结果列表按钮被按下会调用该方法
//- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar ;
//// 11. 搜索框的附属按钮视图中切换按钮会调用该方法
//- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
